const Koa = require('koa')
const app = new Koa()
const json = require('koa-json')
const onerror = require('koa-onerror')
const bodyparser = require('koa-bodyparser')
const logger = require('koa-logger')
const InitManager = require('./config/init')
InitManager.initCore(app)


//跨域处理
const cors = require('./app/middlewares/cors')
app.use(cors)


// 检测token
const auth = require('./app/middlewares/auth')
// app.use(auth)

const api = require('./app/api/v1/index')

// error handler
onerror(app)

// 解析
app.use(bodyparser({
  enableTypes:['json', 'form', 'text']
}))

app.use(json())

// ctx.url
app.use(logger())


// logger
app.use(require('./app/middlewares/logger'))

// routes
app.use(api.routes(), api.allowedMethods())

// error-handling
app.on('error', (err, ctx) => {
  console.error('server error', err, ctx)
})

module.exports = app
